//
//  ServiceZoneViewController.m
//  JiuLe
//
//  Created by xcq on 15/11/17.
//  Copyright © 2015年 Fine. All rights reserved.
//
#import "JlUserManager.h"
#import "SocketManager.h"
#import "ServiceZoneViewController.h"

@implementation ServiceZoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self showNavBarLeftImgAndRightTitle:nil
                            andRightImge:[UIImage imageNamed:@"more_ico"]
                          andRightAction:@selector(RightBtnAction)
                           andLeftAction:@selector(leftBtnAction)
                             andLeftImge:[UIImage imageNamed:@"back_ico"]];
    self.title = @"设置";
    [self setDeviceInfo];
}

- (void)setDeviceInfo {
    _serialNum.text = [JlUserManager shareInstance].userInfo.pid;
    _userNameLb.text = [[NSUserDefaults standardUserDefaults] objectForKey:JiuLeUserName];
    _electricityLb.text = [NSString stringWithFormat:@"%.0f%",[self getBattery] * 100];
    _capacityLb.text = [NSString stringWithFormat:@"%0.1fGB/%0.1fGB",([[ServiceZoneViewController freeDiskSpace] floatValue])/1024/1024/1024,([[ServiceZoneViewController totalDiskSpace] floatValue]/1024/1024/1024)];
    BOOL bol = [[NSUserDefaults standardUserDefaults] boolForKey:JiuLeIsConnection];
    if (bol) {
        _statusLb.text = @"已连接";
    } else {
        _statusLb.text = @"未连接";
    }
}

- (double)getBattery {
    [UIDevice currentDevice].batteryMonitoringEnabled = YES;
    return [UIDevice currentDevice].batteryLevel;
}

+ (NSNumber *)totalDiskSpace
{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    return [fattributes objectForKey:NSFileSystemSize];
}
+ (NSNumber *)freeDiskSpace
{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    return [fattributes objectForKey:NSFileSystemFreeSize];
}

- (IBAction)connect:(id)sender {
    UIButton *btn = (UIButton *)sender;
    if ([SocketManager sharedInstance].socket.isConnected) {
        [[SocketManager sharedInstance] cutOffSocket];
        [SVProgressHUD showSuccessWithStatus:@"断开成功!"];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:JiuLeIsConnection];
        postNotificationWithObject(UserDeviceConnect, @(NO));

    } else {
        [SVProgressHUD showErrorWithStatus:@"设备未连接!"];
    }
}

- (void)RightBtnAction {
    
    
}



- (void)leftBtnAction {
    if (self.navigationController.viewControllers.count == 1) {
        [self homeControllerBackTo];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
